package me.zk.practice1.s;

/**
 * @author zhangkai
 * @date 2023/7/30 14:56
 */
public class LS42 {
    public static int trap(int[] height) {

        int ans = 0;
        int l = 0;
        int r = height.length-1;

        int preMax = 0;

        int sufMax = 0;
        while (l <= r) {

            preMax = Math.max(preMax,height[l]);

            sufMax = Math.max(sufMax,height[r]);

            if (preMax < sufMax) {
                ans += preMax-height[l];
                l++;

            } else {
                ans+= sufMax - height[r];
                r--;
            }

        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(trap(new int[]{0,1,0,2,1,0,1,3,2,1,2,1}));
    }
}
